package ar.edu.unlu.sistemas.ws.test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.xml.namespace.QName;

import ar.edu.unlu.sistemas.movirdroid.business.vo.WordVO;
import ar.edu.unlu.sistemas.ws.client.MovirdroidWebService;
import ar.edu.unlu.sistemas.ws.client.MovirdroidWebServiceImplService;

public class QueryExperiment {
	
	private static final QName SERVICE_NAME = new QName("http://ws.sistemas.unlu.edu.ar/", "MovirdroidWebServiceImplService");
	private static URL wsdlURL = MovirdroidWebServiceImplService.WSDL_LOCATION;
	
	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws ClassNotFoundException 
	 * @throws InterruptedException 
	 */
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException, InterruptedException {
		
		//Levanto el archivo con el vocabulario
		ObjectInputStream ois = new ObjectInputStream( new FileInputStream(new File("/home/pepo/Dropbox/Tesis/Corpus/vocabulario_GRULIC")));
		Map<String,WordVO> words = (HashMap<String,WordVO>) ois.readObject();
		List<WordVO> vocabulary = new LinkedList<WordVO>(words.values());
		//Instancio el RANDOM
		Random random = new Random();
		//Itero queries de 1, 2, y 3 terminos, 50 de cada una
		int index;
		StringBuffer sb;
		for (int i=1;i<=3;i++) {
			for (int j=0;j<50;j++) {
				sb = new StringBuffer();
				for (int k=0;k<i;k++) {
					index = random.nextInt(vocabulary.size());
					sb.append(vocabulary.get(index).getName()+" ");
				}
				//Mando la query al WS
				Thread.sleep(1000);
				doQuery(sb.toString());
			}
		}
        System.exit(0);
	}
	
	private static void doQuery(String query) {
      
        MovirdroidWebServiceImplService ss = new MovirdroidWebServiceImplService(wsdlURL, SERVICE_NAME);
        MovirdroidWebService port = (MovirdroidWebService) ss.getMovirdroidWebServiceImplPort();  
        
        System.out.println("Invoking query...");
        ar.edu.unlu.sistemas.ws.client.PeerWS _query_arg0 = new ar.edu.unlu.sistemas.ws.client.PeerWS();
        _query_arg0.setAddress("localhost");
        _query_arg0.setId("No-Id");
        _query_arg0.setName("Peer de prueba");
        _query_arg0.setPort(0);
        _query_arg0.setStatus(0);
        java.lang.String _query_arg1 = query;
        try {
        	ar.edu.unlu.sistemas.ws.client.ResultsWS _query__return = port.query(_query_arg0, _query_arg1);
            System.out.println("query.result=" + _query__return);
        } catch (Exception e) {
        	  System.out.println("Se realizo la query \""+_query_arg1+"\", ver logs");
        }
      
	}

}
